package com.wifiview.nativelibs;

import com.wifiview.config.Apps;
import com.wifiview.nativelibs.StreamSelf.VideoParams;

import android.graphics.Bitmap;

public class NativeLibs
{
    public static final short FORMAT_VIDEO_MJPEG = 1;
    public static final short FORMAT_VIDEO_H264_I = 2;
    public static final short FORMAT_VIDEO_H264_P = 3;
    public static final short FORMAT_VIDEO_YUYV = 4;


    private long mNativePtr;

    static {
        System.loadLibrary("icamera");
    }

    public NativeLibs()
    {
        // TODO Auto-generated constructor stub
        mNativePtr = nativeCreateCamera(Apps.defaultIpAddr);
    }

    public void destroyCamera() {
        nativeDestroyCamera(mNativePtr);
        mNativePtr = 0;
    }

    public boolean startPreview() {
        return nativeStartPreview(mNativePtr);
    }

    public void stopPreview() {
        nativeStopPreview(mNativePtr);
    }

    public byte[] getOneFrameBuffer() {
        return nativeGetFrameBuffer(mNativePtr);
    }

    public int getVideoFormat(VideoParams obj) {
        return nativeGetVideoFormat(mNativePtr, obj);
    }

    /* Below for Live Stream */
    private static native long nativeCreateCamera(String ip);

    private static native void nativeDestroyCamera(long id_cam);

    private static native boolean nativeStartPreview(long id_cam);

    private static native void nativeStopPreview(long id_cam);

    private static native byte[] nativeGetFrameBuffer(long id_cam);

    public static native byte[] nativeGetPassErrorBuf();

    private static native int nativeGetVideoFormat(long id_cam, VideoParams p);

    /* Below for AVI Record */
    public native static boolean nativeAVIRecStart(String absPath);

    public native static void nativeAVIRecStop();

    public native static void nativeAVIRecSetParams(int w, int h, int fps);

    public native static void nativeAVIRecSetAudioParams(int channel, int sampleRate, int bit);

    public native static void nativeAVIRecAddData(byte[] data, int size);

    public native static void nativeAVIRecAddWav(byte[] data, int size);

    public native static int nativeAVIRecGetTimestamp();

    public native static void nativeAVIRecAddBmp(Bitmap bmp);	//for YUV

    /* Below for AVI Play */
    public native static boolean nativeAVIOpen(String absPath);

    public native static void nativeAVIClose();

    public native static double nativeAVIGetTotalTime();

    public native static int nativeAVIGetTotalFrame();

    public native static byte[] nativeAVIGetFrameAtIndex(int index);

    public native static byte[] nativeAVIGetFrameAtTime(double time);

    public native static byte[] nativeAVIGetVoiceAtTime(double time);


    /* Below for Commands */
    /**
     * 设置设备的wifi名称
     * @param ip    设备ip地址
     * @param wifiName
     * @return
     */
    public native static int nativeCmdSetName(String ip, String wifiName);

    /**
     * 设置设备的wifi密码
     * @param ip    设备ip地址
     * @param password
     * @return
     */
    public native static int nativeCmdSetPassword(String ip, String password);

    /**
     * 清除密码
     * @param ip    设备ip地址
     * @return
     */
    public native static int nativeCmdClearPassword(String ip);

    /**
     * 重启设备
     * @param ip    设备ip地址
     * @return
     */
    public native static int nativeCmdSendReboot(String ip);

    /**
     * 设置分辨率
     * @param ip    设备ip地址
     * @param width
     * @param height
     * @param frameRate
     * @return
     */
    public native static int nativeCmdSetResolution(String ip, int width, int height, int frameRate);

    /**
     * 获取设备分辨率，如何解析byte[]成分辨率参考CmdSocket中的代码
     * @param ip    设备ip地址
     * @return
     */
    public native static byte[] nativeCmdGetResolution(String ip);

    /**
     * 获取设备是支持Key还是串口透传
     * @param ip    设备ip地址
     * @return  1 key, 2 uart
     */
    public native static int nativeCmdGetKeyOrUart(String ip);

    /**
     * 获取按键状态
     * @param ip    设备ip地址
     * @return  1 拍照，2 录像
     */
    public native static int nativeCmdGetRemoteKey(String ip);

    public native static int nativeCmdSetChannel(String ip, int channel);

    /*---------------------- below for station mode ----------------------*/

    /**
     * 设置设备需要连接的路由的信息
     * @param ip    设备ip地址
     * @param mode      0 无加密，1 wep加密（暂不支持），2 wpa加密
     * @param ssid      路由ssid
     * @param pass      路由password
     * @return          1 成功，其他失败
     */
    public native static int nativeCmdSetApParams(String ip, int mode, String ssid, String pass);

    /**
     * 清除设备的路由信息
     * @param ip    设备ip地址
     * @return          1 成功
     */
    public native static int nativeCmdClearApParams(String ip);

    /**
     * 设置设备为网口模式
     * @param ip    设备ip地址
     * @return          1 成功
     */
    public native static int nativeCmdSetEthernet(String ip);

    /*---------------------- below for search device ----------------------*/
    /**
     * 打开搜索局域网内device的socket
     * @return socket fd
     */
    public native static int nativeOpenSearchSocket();

    /**
     * 关闭socket
     * @param sock nativeOpenSearchSocket返回的socket fd
     */
    public native static void nativeCloseSearchSocket(int sock);

    /**
     * 根据ip发送指令
     * @param sock  nativeOpenSearchSocket返回的socket fd
     * @param ip    需要发送到的ip地址
     * @return
     */
    public native static int nativeSendSearchSocket(int sock, String ip);

    /**
     * 接受从socket返回的信息，若有返回，表示此ip地址对应的设备是对的
     * @param sock  nativeOpenSearchSocket返回的socket
     * @return      设备ip地址，NULL则为失败
     */
    public native static String nativeRecvSearchSocket(int sock);






    ////////

    /**
     * 根据搜索到的设备ip打开串口透传
     * @param ip    设备ip地址
     * @return      指针 id_uart
     */
    public native static long nativeUartInit(String ip);

    /**
     * 关闭串口透传
     * @param id_uart nativeUartInit返回的指针
     */
    public native static void nativeUartDeinit(long id_uart);

    /**
     * 发送数据到串口
     * @param id_uart   nativeUartInit返回的指针
     * @param data
     * @param size
     * @return
     */
    public native static int nativeUartSendData(long id_uart, byte[] data, int size);

    /**
     * 接受数据，在物理串口透传时，即单片机通过串口向wifi模块发送数据时，为避免数据过长而被串口
     * 分割，应遵循格式：0xa6 + 数据长度 + 数据内容
     * 如以下数据，data[0]为head，data[1]=0x08为数据长度8，后面8位为数据内容：
     * uint8 data[10] = 0xa6 0x08 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88
     * @param id_uart
     * @return  正常为byte[]，无数据或失败为NULL
     */
    public native static byte[] nativeUartRecvData(long id_uart);

}
